package com.txy.webmanagement.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

/**
 * 阿里云短信发送模板
 *
 * @author wf
 * @date 2019-03-22$
 **/
public class AliSendSmsUtils {


    private static String TemplateCode = "SMS_161595984";
    private static String SignName = "茹峰水泥采购平台";
    private static String accessKeyId = "LTAI7oKlW1Sz9Obl";
    private static String accessSecret = "iu4BDJSjin1vIGfUf2CD0wtRzGPNTK";

    /**
     * 手机短信单条发送信息
     *
     * @param PhoneNumbers
     * @return
     */
    public static Boolean sendSms(String PhoneNumbers, String code) {
        Boolean boo = false;
        DefaultProfile profile = DefaultProfile.getProfile("default", accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("PhoneNumbers", PhoneNumbers);
        request.putQueryParameter("TemplateCode", TemplateCode);
        request.putQueryParameter("SignName", SignName);
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            boo = true;
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return boo;
    }

    /**
     * 批量发送短信
     *
     * @param PhoneNumberJson
     * @param SignNameJson
     * @return
     */
    public static Boolean sendBatchSms(String PhoneNumberJson, String SignNameJson, String TemplateParamJson) {
        Boolean boo = false;

        DefaultProfile profile = DefaultProfile.getProfile("default", accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendBatchSms");
        request.putQueryParameter("SignNameJson", PhoneNumberJson);
        request.putQueryParameter("TemplateCode", TemplateCode);
        request.putQueryParameter("PhoneNumberJson", SignNameJson);
        request.putQueryParameter("TemplateParamJson", TemplateParamJson);
        try {
            CommonResponse response = client.getCommonResponse(request);

        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }


        return boo;
    }

    /**
     * test
     *
     * @param args
     */
    public static void main(String[] args) {
        String PhoneNumbers = "18766079215";
        sendSms(PhoneNumbers, "1090");
    }

}